﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        List<View_RP_Temple> View_RP_Temple_Get(int ACID);

        [OperationContract]
        bool vaildUser(string usercode, string password,int ACID);

        [OperationContract]
        List<View_Emp_lite> View_Emp_lite_List(string shopcode, int ACID);

        [OperationContract]
        View_Emp_lite View_Emp_Lite_by_EmpID(string empcode, int ACID);

        [OperationContract]
        List<View_Client_lite> View_Client_lite_List(bool isdouble, int ACID);

        [OperationContract]
        List<View_Combo_Lite> View_Combo_lite_List(string ClassCode, int ACID);

        [OperationContract]
        string getBillNO(string shopcode, DateTime date, int billtypeid, int ACID);

        [OperationContract]
        List<View_BlankCards> View_BlankCards_List(int ACID);

        [OperationContract]
        List<View_Goods_Lite> View_Goods_Lite_List(string ClassID, int ACID);

        [OperationContract]
        List<View_numphone_Full> View_numphone_Full_List(string ComboCode,string phone,string cno,string state, int ACID);

        [OperationContract]
        List<View_numphone_Lite> View_numphone_Lite_List(string ComboCode, int ACID);

        [OperationContract]
        List<gd> gd_get_By_date(DateTime sd, DateTime ed, int ACID);
    }
    public partial class Service : IService
    {

        public List<gd> gd_get_By_date(DateTime sd, DateTime ed, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.gd.Where(p => p.PDate >= sd && p.PDate <= ed).ToList();
            }
        }

        #region  验证用户
        /// <summary>
        /// 验证用
        /// </summary>
        /// <param name="usercode">员工编码</param>
        /// <returns>bool</returns>
        public bool vaildUser(string usercode, string password,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.Employee.Count(p => p.EmpCode == usercode && p.PassWord == password) == 1;
            }

        }
        #endregion

        public List<View_RP_Temple> View_RP_Temple_Get(int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.View_RP_Temple.ToList();
            }
        }

        public List<View_Emp_lite> View_Emp_lite_List(string shopcode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (string.IsNullOrWhiteSpace(shopcode))
                {
                    return ctx.View_Emp_lite.ToList();
                }
                else
                {
                    return ctx.View_Emp_lite.Where(p => p.shopcode == shopcode).ToList();
                }
            }
        }

        /// <summary>
        /// 取客户简表
        /// </summary>
        /// <param name="isdouble">真为供应商，假为客户</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<View_Client_lite> View_Client_lite_List(bool isdouble, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.View_Client_lite.Where(p=>p.IsDouble==isdouble).ToList();
            }
        }

        public View_Emp_lite View_Emp_Lite_by_EmpID(string empcode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return ctx.View_Emp_lite.Single(p => p.EmpCode == empcode);
                }
                catch (Exception)
                {
                    return null;
                }
                
            }
        }

        /// <summary>
        ///去套餐简表 
        /// </summary>
        /// <param name="ClassCode">套餐分类</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<View_Combo_Lite> View_Combo_lite_List(string ClassCode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (string.IsNullOrEmpty(ClassCode))
                {
                    return ctx.View_Combo_Lite.ToList();
                }
                else
                {
                    return ctx.View_Combo_Lite.Where(p => p.ClassCode.StartsWith(ClassCode)).ToList();
                }
            }
        }

        /// <summary>
        /// 根据规则自动生成单据号
        /// </summary>
        /// <param name="shopcode"></param>
        /// <param name="date"></param>
        /// <param name="billtypeid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string getBillNO(string shopcode, DateTime date, int billtypeid, int ACID)
        {
            string max = "";
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                
                date = DateTime.Parse(date.ToShortDateString());
                int lslength=int.Parse(Sys_ConfigByid_Get(1,ACID).itemvalue);
                if (ctx.Bill.Count(p => p.billtypeid == billtypeid) != 1)
                {
                    return "非法单据类型，获取单号出错！";
                }
                else
                {
                    Bill bill = ctx.Bill.Single(p => p.billtypeid == billtypeid);
                    switch (bill.billtypeid)
                    {
                        case 1:
                            string qz1 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.CardIN_M.Count(p=>p.Pdate.Date==date)>0)
                            {
                                max = (ctx.CardIN_M.Where(p=>p.Pdate.Date==date)).Max(p => p.pid).Replace(qz1,"");
                                max = (Convert.ToInt32(max)+1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz1, max);
                            break;
                        case 2:
                            string qz2 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.BlankCardIN_M.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.BlankCardIN_M.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz2, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz2, max);
                            break;
                        case 3:
                            string qz3 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.CardOpenRQ_M.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.CardOpenRQ_M.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz3, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz3, max);
                            break;
                        case 4:
                            string qz4 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.CardOpen_M.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.CardOpen_M.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz4, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz4, max);
                            break;
                        case 5:
                            string qz5 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.KCSale_M.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.KCSale_M.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz5, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz5, max);
                            break;
                        case 6:
                            string qz6 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.CZ_M.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.CZ_M.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz6, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz6, max);
                            break;
                        case 7:
                            string qz7 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.maintain.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.maintain.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz7, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz7, max);
                            break;
                        case 8:
                            string qz8 = string.Format(bill.demo,  string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.whd_M.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.whd_M.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz8, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz8, max);
                            break;
                        case 9:
                            string qz9 = string.Format(bill.demo, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.wsd.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.wsd.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz9, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz9, max);
                            break;
                        case 10:
                            string qz10 = string.Format(bill.demo, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.gd.Count(p => p.PDate.Date == date) > 0)
                            {
                                max = (ctx.gd.Where(p => p.PDate.Date == date)).Max(p => p.pid).Replace(qz10, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz10, max);
                            break;
                        case 11: //收款单

                            string qz11 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.SK.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.SK.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz11, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz11, max);
                            break;
                        case 12: //付款单

                            string qz12 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.FK.Count(p => p.Pdate.Date == date) > 0)
                            {
                                max = (ctx.FK.Where(p => p.Pdate.Date == date)).Max(p => p.pid).Replace(qz12, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz12, max);
                            break;
                        case 13: //产品销售单
                            string qz13 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.ProductSale_M.Count(p => p.odate.Date == date) > 0)
                            {
                                max = (ctx.ProductSale_M.Where(p => p.odate.Date == date)).Max(p => p.djn).Replace(qz13, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz13, max);
                            break;
                        case 14: //产品销售退货单
                            string qz14 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.ProductSaleBack_M.Count(p => p.odate.Date == date) > 0)
                            {
                                max = (ctx.ProductSaleBack_M.Where(p => p.odate.Date == date)).Max(p => p.djn).Replace(qz14, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz14, max);
                            break;
                        case 15:
                            string qz15 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.ProductIN_M.Count(p => p.billdate.Date == date) > 0)
                            {
                                max = (ctx.ProductIN_M.Where(p => p.billdate.Date == date)).Max(p => p.billid).Replace(qz15, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz15, max);
                            break;
                        case 16:
                            string qz16 = string.Format(bill.demo, shopcode, string.Format("{0:yyMMdd}", date, ACID));
                            if (ctx.ProductIN_M.Count(p => p.billdate.Date == date) > 0)
                            {
                                max = (ctx.ProductIN_M.Where(p => p.billdate.Date == date)).Max(p => p.billid).Replace(qz16, "");
                                max = (Convert.ToInt32(max) + 1).ToString().PadLeft(lslength, '0');
                            }
                            else
                            {
                                max = "1".PadLeft(lslength, '0');
                            }
                            max = string.Format("{0}{1}", qz16, max);
                            break;
                        default:
                            max = "未知单据类型,获取单号出错！";
                            break;
                    }


                }
            }
            return max;
        }

        public List<View_BlankCards> View_BlankCards_List(int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.View_BlankCards.ToList();
            }
        }

        public List<View_Goods_Lite> View_Goods_Lite_List(string ClassID,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (string.IsNullOrEmpty(ClassID))
                {
                    return ctx.View_Goods_Lite.ToList();
                }
                else
	            {
                    return ctx.View_Goods_Lite.Where(p => p.ClassID == ClassID).ToList();
	            }
            }
        }

        public List<View_numphone_Full> View_numphone_Full_List(string ComboCode, string phone, string cno, string state, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<View_numphone_Full, bool>> predicate = PredicateBuilder.True<View_numphone_Full>();
                if (!string.IsNullOrEmpty(phone))
                {
                    predicate=p => p.号码 == phone;
                }
                if (!string.IsNullOrEmpty(cno))
                {
                    predicate = predicate.And(p => p.串号 == cno);
                }
                if (!string.IsNullOrEmpty(state))
                {
                    predicate = predicate.And(p => p.状态 == state);
                }
                if (!string.IsNullOrEmpty(ComboCode))
                {
                    predicate = predicate.And(p => p.套餐编号 == ComboCode);
                }
                List<View_numphone_Full> ps = ctx.View_numphone_Full.Where(predicate).ToList();
                return ps;
            }
        }

        public List<View_numphone_Lite> View_numphone_Lite_List(string ComboCode, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                if (string.IsNullOrEmpty(ComboCode))
                {
                    return ctx.View_numphone_Lite.ToList();
                }
                else
                {
                    return ctx.View_numphone_Lite.Where(p => p.ComboCode == ComboCode).ToList();
                }

            }
        }
        /// <summary>
        /// 事物用记录应收账款方法
        /// </summary>
        /// <param name="balance">应收金额/应付金额</param>
        /// <param name="clientID">客户ID/供应商ID</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="ACID">帐套ID</param>
        private void RecordBalance(decimal balance, Guid clientID, DataClassesDataContext ctx, int ACID)
        {
            CRM_Client client = ctx.CRM_Client.Single(p => p.ClientID == clientID);
            client.balance += balance;
        }

        /// <summary>
        /// 事物用记录应收账款方法
        /// </summary>
        /// <param name="balance">预收付金额/预付金额</param>
        /// <param name="clientID">客户ID/供应商ID</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="ACID">帐套ID</param>
        private void Recordsuramt(decimal suramt, Guid clientID, DataClassesDataContext ctx, int ACID)
        {
            CRM_Client client = CRM_ClientByClientID_Get(clientID, ACID);
            client.balance += suramt;
        }

        private void suramt2Balance(decimal suramt, Guid clientID, DataClassesDataContext ctx, int ACID)
        {
            CRM_Client client = CRM_ClientByClientID_Get(clientID, ACID);
            client.balance -= suramt;
            client.suramt -= suramt;
        }
    }


}

